package com.zpphis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpphis.entity.User;
import com.zpphis.service.IMenuService;
import com.zpphis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class AdminController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IMenuService menuService;

    @RequestMapping("/login.json")
    public Map<String, Object> login(String username, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        lqw.eq(User::getPassword, password);
        User user = userService.getOne(lqw);
        if (user == null) {
            map.put("code", 0);
            map.put("msg", "用户名或密码错误！");
        } else {
            session.setAttribute("LoginUser", user);
            map.put("code", 1);
        }

        return map;
    }

    @RequestMapping("/init.json")
    public Map<String, Object> init(HttpSession session) {
        User user = (User) session.getAttribute("LoginUser");
        Map<String, Object> map = new HashMap<>();
        if (user == null) {
            map.put("isLogin", 0);
            return map;
        } else {
            map.put("isLogin", 1);
        }
        map.put("homeInfo", new HashMap<String, Object>() {
            {
                put("title", "首页");
                put("href", "page/welcome.html");
            }
        });
        map.put("logoInfo", new HashMap<String, Object>() {
            {
                put("title", "ZPP - HIS");
                put("image", "images/logo.png");
            }
        });
        map.put("menuInfo", menuService.buildMenu(user.getUserTypeId()));

        return map;
    }

    @RequestMapping("/repwd.json")
    public Map<String, Object> repwd(String old_password, String new_password, String again_password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("LoginUser");
        if (user == null || !user.getPassword().equals(old_password)) {
            map.put("code", 0);
            map.put("msg", "原密码错误！");
        } else if (!new_password.equals(again_password)) {
            map.put("code", 0);
            map.put("msg", "两次密码不一致！");
        } else {
            user.setPassword(new_password);
            userService.updateById(user);
            session.setAttribute("LoginUser", user);
            map.put("code", 1);
            map.put("msg", "密码修改成功！");
        }
        return map;
    }

    @RequestMapping("/logout.json")
    public void logout(HttpSession session) {
        session.invalidate();
    }
}
